package com.xnyzc.lhy.mis.controller.cmsapi.user.amount;

import com.xnyzc.lhy.common.entity.Rv;
import com.xnyzc.lhy.common.exception.PangException;
import com.xnyzc.lhy.common.penum.EErrorCode;
import com.xnyzc.lhy.common.util.CheckUtil;
import com.xnyzc.lhy.common.util.TokenUtil;
import com.xnyzc.lhy.mis.entity.param.cms.amount.DistributionAmountParam;
import com.xnyzc.lhy.mis.entity.param.cms.amount.RechargeOfficeAmountParam;
import com.xnyzc.lhy.mis.mapper.system.OaSysUserOfficeMapper;
import com.xnyzc.lhy.mis.service.cms.amount.IOaSysDistributionAmountService;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.math.BigDecimal;
import java.util.Date;
import java.util.HashMap;
import java.util.List;

/**
 * @Description 分配额度
 * @Author zhuyubin
 * @Date 2019-08-22 16:35
 **/
@RestController
@RequestMapping(value = "cmsapi/distributionAmount")
public class OaSysDistributionAmountController {

    @Autowired
    private IOaSysDistributionAmountService iOaSysDistributionAmountService;
    @Autowired
    private OaSysUserOfficeMapper oaSysUserOfficeMapper;

    /**
     * 根据officeId获取子节点
     * @param officeId 机构id
     * @return
     */
    @GetMapping(value = "selectOfficeOrUserByOfficeId")
    public Rv selectOfficeOrUserByOfficeId(
            @ApiParam(name = "officeId", value = "机构id") Long officeId,
            @ApiParam(name = "current", value = "当前页") Long current,
            @ApiParam(name = "name", value = "用户名或机构名") @RequestParam(required = false) String name,
            @ApiParam(name = "phone", value = "用户手机号") @RequestParam(required = false) String phone,
            @ApiParam(name = "searchOfficeId", value = "查询的部门id") @RequestParam(required = false) Long searchOfficeId){
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        if (CheckUtil.objIsEmpty(officeId)){
            throw PangException.create(EErrorCode.unSysUserOffice);
        }
        return Rv.wrap(iOaSysDistributionAmountService.selectOfficeOrUserByOfficeId(officeId, tag, current, name, phone, searchOfficeId));
    }

    /**
     * 分配额度
     * @param distributionAmountParam 入参
     * @return
     */
    @PostMapping(value = "distributionAmount")
    public Rv distributionAmount(@ApiParam(name = "distributionAmountParam", value = "分配额度入参") @RequestBody DistributionAmountParam distributionAmountParam){
        long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Long officeId = distributionAmountParam.getOfficeId();
        List<Long> ids = distributionAmountParam.getIds();
        BigDecimal amount = distributionAmountParam.getAmount();
        Integer amountType = distributionAmountParam.getAmountType();
        if (CheckUtil.objIsEmpty(officeId, ids, amount, amount, amountType)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(EErrorCode.success, iOaSysDistributionAmountService.distributionAmount(userId, officeId, ids, amount, tag, amountType), new HashMap<>(16));
    }

    /**
     * 查询额度明细
     * @param officeId 机构id
     * @param amountType 额度类型
     * @param starTime 开始时间
     * @param name 机构名
     * @return
     */
    @GetMapping(value = "selectOfficeRecord")
    public Rv selectOfficeRecord(
            @ApiParam(name = "officeId", value = "机构id") @RequestParam(value = "officeId") Long officeId,
            @ApiParam(name = "amountType", value = "额度类型") @RequestParam(value = "amountType") Integer amountType,
            @ApiParam(name = "current", value = "当前页") @RequestParam(value = "current") Long current,
            @ApiParam(name = "starTime", value = "开始时间") @RequestParam(required = false) List<Date> starTime,
            @ApiParam(name = "name", value = "用户或机构名") @RequestParam(value = "name", required = false) String name,
            @ApiParam(name = "createName", value = "用户或机构名") @RequestParam(value = "createName", required = false) String createName,
            @ApiParam(name = "searchOfficeId", value = "查询机构id") @RequestParam(value = "searchOfficeId", required = false) Long searchOfficeId,
            @ApiParam(name = "size", value = "一页数量") @RequestParam(value = "size", required = false) Long size
    ){
        Long userId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysDistributionAmountService.selectOfficeRecord(userId, tag, officeId, amountType, current, starTime, name, createName, size, searchOfficeId));
    }

    /**
     * 充值额度
     * @param rechargeOfficeAmountParam 充值额度入参
     * @return
     */
    @PostMapping(value = "rechargeOfficeAmount")
    public Rv rechargeOfficeAmount(@ApiParam(name = "rechargeOfficeAmountParam", value = "充值额度入参") @RequestBody RechargeOfficeAmountParam rechargeOfficeAmountParam){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        Long officeId = rechargeOfficeAmountParam.getOfficeId();
        String personalAmount = rechargeOfficeAmountParam.getPersonalAmount();
        String officeAmount = rechargeOfficeAmountParam.getOfficeAmount();
        if (CheckUtil.objIsEmpty(officeId)){
            throw PangException.create(EErrorCode.missingArg);
        }
        return Rv.wrap(iOaSysDistributionAmountService.rechargeOfficeAmount(sysUserId, tag, officeId, personalAmount, officeAmount));
    }

    /**
     * 分配额度搜索部门树
     * @param officeId
     * @return
     */
    @GetMapping(value = "/searchOfficeTreeMap")
    public Rv searchOfficeTreeMap(@ApiParam(name = "officeId", value = "机构id") @RequestParam Long officeId){
        Long sysUserId = TokenUtil.getTokenUserId();
        String tag = TokenUtil.getTag();
        if (CheckUtil.objIsEmpty(tag)){
            throw PangException.create(EErrorCode.missingAuthArgs);
        }
        return Rv.wrap(iOaSysDistributionAmountService.searchOfficeTreeMap(sysUserId, tag, officeId));
    }
}
